package com.itheima.controller;


import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
@SessionAttributes(names = {"age","gender"})
public class ServletController {

    //获取request,response,session对象的原生接口
    @RequestMapping("/serletApi")
    public String serletApi(HttpServletRequest request, HttpServletResponse response, HttpSession session){
        System.out.println(request);
        System.out.println(response);
        System.out.println(session);
        return "page";
    }

    //获取head数据的快捷操作方式
    @RequestMapping("/headApi")
    public String headApi(@RequestHeader("Accept-Encoding") String header) {
        System.out.println(header);
        return "page";
    }

    //获取cookie数据的快捷操作方式
    @RequestMapping("/cookieApi")
    public String cookieApi(@CookieValue("JSESSIONID") String jsessionid){
        System.out.println(jsessionid);
        return "page";
    }

    @RequestMapping("/setSessionData")
    //Model对象实现数据set需要配合@SessionAttributes(names = {"age","gender"})使用
    //将数据放入session存储范围，通过Model对象实现数据set，通过@SessionAttributes注解(在类上方)实现范围设定
    public String setSessionData(HttpSession session, Model model){
        session.setAttribute("name","itheima");
        model.addAttribute("age",22);
        model.addAttribute("gender","man");
        return "page";
    }

    @RequestMapping("/getSessionData")
    public String getSessionData(@SessionAttribute("name") String name,
                                 @SessionAttribute("age") int age,
                                 @SessionAttribute("gender") String gender){
        System.out.println(name);
        System.out.println(age);
        System.out.println(gender);
        return "page";
    }
}
